import 'package:edu/constants/app_strings.dart';
import 'package:edu/constants/config.dart';
import 'package:edu/networks/common/base_response.dart';
import 'package:edu/networks/common/page_meta.dart';
import 'package:json_annotation/json_annotation.dart';

part 'course_response.g.dart';

@JsonSerializable()
class CourseSubcategory {
  @JsonKey(name: 'id')
  int id;
  @JsonKey(name: 'title')
  String title;
  @JsonKey(name: 'icon')
  String icon;

  CourseSubcategory({
    required this.id,
    required this.title,
    required this.icon,
  });

  factory CourseSubcategory.fromJson(Map<String, dynamic> json) =>
      _$CourseSubcategoryFromJson(json);

  Map<String, dynamic> toJson() => _$CourseSubcategoryToJson(this);
}

@JsonSerializable()
class CourseCategory {
  @JsonKey(name: 'id')
  int id;
  @JsonKey(name: 'title')
  String title;
  @JsonKey(name: 'icon')
  String icon;
  @JsonKey(name: 'subs')
  List<CourseSubcategory> subCategories;

  CourseCategory({
    required this.id,
    required this.title,
    required this.icon,
    required this.subCategories,
  });

  factory CourseCategory.fromJson(Map<String, dynamic> json) =>
      _$CourseCategoryFromJson(json);

  Map<String, dynamic> toJson() => _$CourseCategoryToJson(this);
}

@JsonSerializable()
class CourseCategoryResponse extends BaseResponse {
  @JsonKey(name: 'categories')
  List<CourseCategory>? categories;

  CourseCategoryResponse({required super.code, super.message, this.categories});

  factory CourseCategoryResponse.fromJson(Map<String, dynamic> json) =>
      _$CourseCategoryResponseFromJson(json);

  Map<String, dynamic> toJson() => _$CourseCategoryResponseToJson(this);
}

@JsonSerializable()
class TopCourseKeysResponse extends BaseResponse {
  @JsonKey(name: 'keys')
  List<String>? keys;

  TopCourseKeysResponse({required super.code, super.message, this.keys});

  factory TopCourseKeysResponse.fromJson(Map<String, dynamic> json) =>
      _$TopCourseKeysResponseFromJson(json);

  Map<String, dynamic> toJson() => _$TopCourseKeysResponseToJson(this);
}

@JsonSerializable()
class CourseBrief {
  @JsonKey(name: 'id')
  int id;
  // 标题
  @JsonKey(name: 'title')
  String title;
  // 图片
  @JsonKey(name: 'image')
  String image;
  // 视频数量
  @JsonKey(name: 'video_count')
  int videoCount;
  // 课程时长
  @JsonKey(name: 'duration')
  int duration;
  // 等级
  @JsonKey(name: 'level')
  String level;
  // 价格
  @JsonKey(name: 'price')
  double price;
  // 作者
  @JsonKey(name: 'author')
  String author;
  // 作者id
  @JsonKey(name: 'author_id')
  int authorId;
  // 学生数量
  @JsonKey(name: 'student_count')
  int studentCount;

  CourseBrief({
    required this.id,
    required this.title,
    required this.image,
    required this.videoCount,
    required this.duration,
    required this.level,
    required this.price,
    required this.author,
    required this.authorId,
    required this.studentCount,
  });

  factory CourseBrief.fromJson(Map<String, dynamic> json) =>
      _$CourseBriefFromJson(json);
  Map<String, dynamic> toJson() => _$CourseBriefToJson(this);

  // 课程图片
  String get imageUrl => '${Config.baseUrl}/v1/course/image/$image';

  // 课程等级
  String get levelTitle {
    switch (level) {
      case 'beginner':
        return AppStrings.beginner;
      case 'intermediate':
        return AppStrings.intermediate;
      case 'advanced':
        return AppStrings.advanced;
      default:
        return '未知';
    }
  }

  // 课程时长
  int get durationInMinutes => duration ~/ 60;
  // 课程时长毫秒数
  int get durationInMilliseconds => duration * 1000;
}

@JsonSerializable()
class CourseListResponse extends BaseResponse {
  @JsonKey(name: 'courses')
  List<CourseBrief>? courses;

  @JsonKey(name: 'page_meta')
  PageMeta? pageMeta;

  CourseListResponse({
    required super.code,
    super.message,
    this.courses,
    this.pageMeta,
  });

  factory CourseListResponse.fromJson(Map<String, dynamic> json) =>
      _$CourseListResponseFromJson(json);

  Map<String, dynamic> toJson() => _$CourseListResponseToJson(this);
}

// 课程详情

// 视频列表
@JsonSerializable()
class VideoItem {
  @JsonKey(name: 'id')
  int id;
  // 索引
  @JsonKey(name: 'index')
  int index;
  // 标题
  @JsonKey(name: 'title')
  String title;
  // 持续时间
  @JsonKey(name: 'duration')
  int duration;

  VideoItem({
    required this.id,
    required this.index,
    required this.title,
    required this.duration,
  });

  factory VideoItem.fromJson(Map<String, dynamic> json) =>
      _$VideoItemFromJson(json);

  Map<String, dynamic> toJson() => _$VideoItemToJson(this);
}

// 章节列表
@JsonSerializable()
class ChapterItem {
  @JsonKey(name: 'id')
  int id;
  // 索引
  @JsonKey(name: 'index')
  int index;
  // 标题
  @JsonKey(name: 'title')
  String title;
  // 视频列表
  @JsonKey(name: 'videos')
  List<VideoItem>? videos;

  ChapterItem({
    required this.id,
    required this.index,
    required this.title,
    this.videos,
  });

  factory ChapterItem.fromJson(Map<String, dynamic> json) =>
      _$ChapterItemFromJson(json);

  Map<String, dynamic> toJson() => _$ChapterItemToJson(this);
}

// 课程作者或讲师
@JsonSerializable()
class CourseAuthor {
  // id
  @JsonKey(name: 'id')
  int id;
  // 名称
  @JsonKey(name: 'name')
  String name;
  // 介绍
  @JsonKey(name: 'introduction')
  String? introduction;
  // 自我申论
  @JsonKey(name: 'self_apply')
  String? selfApplyedTitle;
  // 课程学习
  @JsonKey(name: 'learn_mark')
  String? learnMarkTitle;
  // 课程销售
  @JsonKey(name: 'sell_mark')
  String? sellMarkTitle;
  // 等级
  @JsonKey(name: 'level_mark')
  String? levelMarkTitle;

  CourseAuthor({
    required this.id,
    required this.name,
    this.introduction,
    this.selfApplyedTitle,
    this.learnMarkTitle,
    this.sellMarkTitle,
    this.levelMarkTitle,
  });

  factory CourseAuthor.fromJson(Map<String, dynamic> json) =>
      _$CourseAuthorFromJson(json);

  Map<String, dynamic> toJson() => _$CourseAuthorToJson(this);
}

// 课程评价
@JsonSerializable()
class CourseReviewSummary {
  // 总数
  @JsonKey(name: 'total_count')
  int totalCount;
  // 平均评分
  @JsonKey(name: 'avg_rating')
  double? avgRating;
  // 评分
  @JsonKey(name: 'rating_counts')
  Map<int, int>? ratingCounts;

  CourseReviewSummary({
    required this.totalCount,
    this.avgRating,
    this.ratingCounts,
  });

  factory CourseReviewSummary.fromJson(Map<String, dynamic> json) =>
      _$CourseReviewSummaryFromJson(json);

  Map<String, dynamic> toJson() => _$CourseReviewSummaryToJson(this);
  // 是否有评分
  bool get hasRatings => totalCount > 0;
  // 评分
  double get rating => avgRating ?? 0;
  // 评分字符串
  String get ratingString => '${(avgRating ?? 0).toStringAsFixed(1)} / 5.0';
}

// 课程详情
@JsonSerializable()
class CourseDetail {
  // 课程id
  @JsonKey(name: 'id')
  int id;
  // 课程标题
  @JsonKey(name: 'title')
  String title;
  // 课程图片
  @JsonKey(name: 'image')
  String image;
  // 视频数量
  @JsonKey(name: 'video_count')
  int videoCount;
  // 课程时长
  @JsonKey(name: 'duration')
  int duration;
  // 课程等级
  @JsonKey(name: 'level')
  String level;
  // 价格
  @JsonKey(name: 'price')
  double price;
  // 课程描述
  @JsonKey(name: 'description')
  String description;
  // 技能
  @JsonKey(name: 'skills')
  List<String> skills;
  // 章节列表
  @JsonKey(name: 'contents')
  List<ChapterItem> chapters;
  // 课程作者
  @JsonKey(name: 'author')
  CourseAuthor author;
  // 课程学生数
  @JsonKey(name: 'student_count')
  int studentCount;
  // 课程评价
  @JsonKey(name: 'review_summary')
  CourseReviewSummary reviewSummary;
  // 是否收藏
  @JsonKey(name: 'is_favorite')
  bool isFavorite;

  CourseDetail({
    required this.id,
    required this.title,
    required this.image,
    required this.videoCount,
    required this.duration,
    required this.level,
    required this.price,
    required this.description,
    required this.skills,
    required this.chapters,
    required this.author,
    required this.studentCount,
    required this.reviewSummary,
    required this.isFavorite,
  });

  factory CourseDetail.fromJson(Map<String, dynamic> json) =>
      _$CourseDetailFromJson(json);

  Map<String, dynamic> toJson() => _$CourseDetailToJson(this);

  // 图片url
  String get imageUrl => '${Config.baseUrl}/v1/course/image/$image';

  // 作者标题
  String get minorTitle {
    return '${author.name} | ${author.selfApplyedTitle ?? author.levelMarkTitle ?? author.learnMarkTitle ?? ''}';
  }

  // 课程等级标题
  String get levelTitle {
    switch (level) {
      case 'beginner':
        return AppStrings.beginner;
      case 'intermediate':
        return AppStrings.intermediate;
      case 'advanced':
        return AppStrings.advanced;
      default:
        return '';
    }
  }

  // 课程时长
  String get durationInHours {
    final hours = duration ~/ 3600;
    return '$hours${AppStrings.hour}';
  }
}

// 课程详情响应
@JsonSerializable()
class CourseDetailResponse extends BaseResponse {
  @JsonKey(name: 'course')
  CourseDetail? course;

  CourseDetailResponse({required super.code, super.message, this.course});

  factory CourseDetailResponse.fromJson(Map<String, dynamic> json) =>
      _$CourseDetailResponseFromJson(json);

  Map<String, dynamic> toJson() => _$CourseDetailResponseToJson(this);
}
